package com.example.helloworld;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.LinearLayout;

public class PageSliding extends Activity {

	boolean isPageOpen =	false;
	Button startSlide;
	
	private LinearLayout slidePage;
	private Animation translateLeft;
	private Animation translateRight;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_page_sliding);
		
		slidePage =			(LinearLayout)findViewById(R.id.slidePage);
		
		translateLeft =		AnimationUtils.loadAnimation(this, R.anim.translate_left);
		translateRight =	AnimationUtils.loadAnimation(this, R.anim.translate_right);
		
		SlidingPageAnimationListener listener =	new SlidingPageAnimationListener();
		translateLeft.setAnimationListener(listener);
		translateRight.setAnimationListener(listener);
		
		slidePage.startAnimation(translateLeft);
		
		startSlide =	(Button)findViewById(R.id.startSlide);
		startSlide.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (isPageOpen) {
					slidePage.startAnimation(translateRight);
				} else {
					slidePage.setVisibility(View.VISIBLE);
					slidePage.startAnimation(translateLeft);
				}
			}
			}
		);
	}

	private class SlidingPageAnimationListener implements AnimationListener {
		@Override
		public void onAnimationEnd(Animation animation) {
			if (isPageOpen) {
				slidePage.setVisibility(View.INVISIBLE);
				
				startSlide.setText("Open");
				isPageOpen =	false;
			} else {
				startSlide.setText("Close");
				isPageOpen =	true;
			}
			Log.d("end", Boolean.toString(isPageOpen) + "," + animation.toString());
		}

		@Override
		public void onAnimationRepeat(Animation animation) { }

		@Override
		public void onAnimationStart(Animation animation) {
			Log.d("start", Boolean.toString(isPageOpen) + "," + animation.toString());
		}
	}
}
